﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdEnterInplaceEditMode.h"
#include "CmdInplaceEditNameDefines.h"
#include "GmUiInplaceEditCommandRegister.h"
#include "GbmpUiPlatformCommandIds.h"
#include "InplaceEditSharedContent.h"
#include "UiInplaceEditModeProjectUtil.h"

// 平台头文件
#include "IElement.h"
#include "IDocument.h"
#include "IModelView.h"
#include "IInstanceType.h"
#include "IFamily.h"
#include "IFamilyConfigElement.h"
#include "IGraphicsNodeReference.h"
#include "IElementBasicInformation.h"
#include "IPositionGeometry.h"
#include "IElementPosition.h"
#include "ISelection.h"
#include "IInstance.h"
#include "GmProcessServer.h"
#include "QProcess"
#include "CmdStatusUtils.h"
#include "GcmpBuiltInCategoryUniIdentities.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"


using namespace gcmp;

REGISTER_GM_COMMAND(CmdEnterInplaceEditMode)

CmdEnterInplaceEditMode::CmdEnterInplaceEditMode()
    : GbmpCommandBase(ID_CMD_ENTER_INPLACE_EDIT_MODE, true)
{
}

bool gcmp::CmdEnterInplaceEditMode::IsEnabled() const
{
    IInstance* pInstance = GetSelectedInstance();
    if (pInstance && pInstance->GetElementId().IsValid())
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool gcmp::CmdEnterInplaceEditMode::IsVisible() const
{
    CMD_ONLY_VISIBLE_IN_PROJECT_ENVIRONMENT
}

OwnerPtr<IAction> CmdEnterInplaceEditMode::ExecuteCommand(const CommandParameters& cmdParams)
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    IInstance* pSelectedInstance = GetSelectedInstance();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pSelectedInstance, L"pSelectedInstance为空",L"GDMPLab",L"2024-03-30");
    ElementId selectedInstanceId = pSelectedInstance->GetElementId();
    if (selectedInstanceId.IsValid())
    {
        const IElement* pSelectedElement = pDoc->GetElement(selectedInstanceId);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pSelectedElement, L"pSelectedElement为空",L"GDMPLab",L"2024-03-30");

        UniIdentity categoryUid = pSelectedElement->GetBasicInformation()->GetCategoryUid();

        // 获取族文件的路径
        ElementId instanceTypeId = pSelectedElement->GetBasicInformation()->GetTypeId();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(instanceTypeId.IsValid(), L"instanceTypeId无效",L"GDMPLab",L"2024-03-30");
        const IInstanceType* pInstanceType = quick_cast<const IInstanceType>(pDoc->GetElement(instanceTypeId));
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstanceType, L"pInstanceType为空",L"GDMPLab",L"2024-03-30");
        const IFamily* pFamily = pInstanceType->GetFamily();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pFamily, L"pFamily为空",L"GDMPLab",L"2024-03-30");

        const std::wstring familyName = pFamily->GetBasicInformation()->GetName();
        const IFamilyConfigElement* pFamilyConfigElement = IFamilyConfigElement::GetFamilyConfigElementByName(pDoc, familyName);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pFamilyConfigElement, L"对应FamilyConfigItem不存在",L"GDMPLab",L"2024-03-30");
        std::wstring familyFilePath = pFamilyConfigElement->GetFamilyFilePath(pDoc);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(!familyFilePath.empty(), L"族文档路径为空",L"GDMPLab",L"2024-03-30");

        // 获取插入点（即点式族的控制点）
        const IElementPosition* pElementPosition = pSelectedElement->GetElementPosition();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElementPosition, L"pElementPosition为空",L"GDMPLab",L"2024-03-30");
        const IPositionGeometry* pPositionGeometry = pElementPosition->GetPositionGeometry();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPositionGeometry, L"pPositionGeometry2d为空",L"GDMPLab",L"2024-03-30");
        Vector3d insertPt = pPositionGeometry->GetPointByIndex(0);

        // 共享数据的生成
        OwnerPtr<InplaceEditSharedContent> opInplaceEditSharedContent = NEW_AS_OWNER_PTR(InplaceEditSharedContent);
        opInplaceEditSharedContent->SetCategoryId(categoryUid);
        opInplaceEditSharedContent->SetEditingElementId(selectedInstanceId);
        opInplaceEditSharedContent->SetFamilyName(familyName);
        opInplaceEditSharedContent->SetEditFamilyFilePath(familyFilePath);
        opInplaceEditSharedContent->SetInsertionPoint(insertPt);
        std::vector<OwnerPtr<InplaceEditSharedModelViewInfo>> viewList;
        UiInplaceEditModeProjectUtil::GetViewList(pDoc,insertPt, viewList);
        opInplaceEditSharedContent->SetViewInfos(std::move(viewList));
        opInplaceEditSharedContent->SetCurrentViewName(pModelView->GetName());

        // 开始在位编辑
        UiInplaceEditModeProjectUtil::StartInplaceEditProject(pDoc, TransferOwnership(opInplaceEditSharedContent), false);
    }

    return nullptr;
}

IInstance* CmdEnterInplaceEditMode::GetSelectedInstance() const
{
    const std::set<OwnerPtr<IGraphicsNodeReference>, OwnerPtrContainerUtil::LessPredicate<IGraphicsNodeReference> >& selectedObjs
        = ISelection::Get()->GetGraphicsNodeReferences();
    if (selectedObjs.size() == 1)
    {
        const IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
        if (!pDoc)
        {
            return nullptr;
        }

        ElementId elementId = (*selectedObjs.begin())->GetElementId();
        IInstance* pInstance = quick_cast<IInstance>(pDoc->GetElement(elementId));
        if (pInstance && pInstance->GetBasicInformation()->GetCategoryUid() == BuiltInCategoryUniIdentities::BICU_POINT_FAMILY)
        {
            return pInstance;
        }
    }

    return nullptr;
}
